package com.biqee.service.tb.impl;

import com.biqee.dao.TbShopDAO;
import com.biqee.exception.Assert;
import com.biqee.model.db.TbItem;
import com.biqee.model.db.TbShop;
import com.biqee.service.tb.TbShopService;

public class TbShopServiceImpl implements TbShopService {

	@Override
	public Integer save(TbShop tbShop) {
		Assert.assertNotNull(tbShop);
		Integer id = tbShop.getId();
		if(id != null) {
			update(tbShop);
			return id;
		} else {
			String tbShopId = tbShop.getTbShopId();
			TbShop tbShopInDb = readByTbShopId(tbShopId);
			if(tbShopInDb == null) {
				return create(tbShop);
			} else {
				return tbShopInDb.getId();
			}
		}
	}

	@Override
	public TbShop readByTbShopId(String tbShopId) {
		return tbShopDAO.readByTbShopId(tbShopId);
	}

	@Override
	public Integer create(TbShop tbShop) {
		return tbShopDAO.create(tbShop);
	}

	@Override
	public int update(TbShop tbShop) {
		return tbShopDAO.update(tbShop);
	}


	private TbShopDAO tbShopDAO;

	public void setTbShopDAO(TbShopDAO tbShopDAO) {
		this.tbShopDAO = tbShopDAO;
	}

	@Override
	public void savaOrUpdateByShopUrl(TbItem tbItem) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String getFinalGrade(TbShop tbShop) {
		if(isMallShop(tbShop)) {
			return tbShop.getFinalGrade();
		} else {
			return tbShop.getSellerGrade();
		}
	}

	@Override
	public boolean isMallShop(TbShop tbShop) {
		return "0".equals(tbShop.getType());
	}
	
	
}
